package com.labtalk.admin.config;

import com.labtalk.admin.config.component.JwtAuthenticationTokenFilter;
import com.labtalk.admin.config.component.RestAccessDeniedHandler;
import com.labtalk.admin.config.component.RestAuthenticationEntryPoint;
import com.labtalk.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private RestAccessDeniedHandler restAccessDeniedHandler;

    @Autowired
    private RestAuthenticationEntryPoint restAuthenticationEntryPoint;

    // 重写UserDetailsService
    @Override
    @Bean
    protected UserDetailsService userDetailsService() {
        return name -> userService.getUserByName(name);
    }

    // 使用BCryptPasswordEncoder进行密码的加解密
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    // 让spring security走重写的UserDetailsService和PasswordEncoder
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(userDetailsService())
                .passwordEncoder(passwordEncoder());
    }

    // Spring Security配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .headers()
                .cacheControl();
        // 将jwtAuthenticationTokenFilter拦截器放在UsernamePasswordAuthenticationFilter拦截器之前
        // 除被Spring Security放行的接口路径，其余路径会先经过jwt登录拦截过滤器jwtAuthenticationTokenFilter，再执行http.exceptionHandling
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        http.exceptionHandling()
                .accessDeniedHandler(restAccessDeniedHandler)
                .authenticationEntryPoint(restAuthenticationEntryPoint);
        http.cors();
    }

    // Spring Security拦截放行路径
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers(
                "/api/login",
                "/api/getPassword"
        );
    }
}
